﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using _4T.BLL;
using _4T.Data;
using CMS.Core.Libs;

namespace _4T
{
    public partial class CMS_AddProfile : System.Web.UI.Page
    {
        private List<string> imageArrayPath = new List<string>();
        private List<string> videoArrayPath = new List<string>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["imageArrayPath"] = imageArrayPath;
                Session["videoArrayPath"] = videoArrayPath;
            }
            /*if (Request.QueryString["p"] != null)
            {
                string profileCode = Request.QueryString["p"].ToString();
                LoadProfileInfo(profileCode);
                hdfProfileCode.Value = profileCode;
            }
            else
            {*/
            //txtDOB.Text = DateTime.Now.ToShortDateString();
            //txtMedicalTime.Text = DateTime.Now.ToShortDateString();
            //txtCreatedDate.Text = DateTime.Now.ToShortDateString();
            /*}*/
        }

        /*private void LoadProfileInfo(string profileCode)
        {
            ProfileInfoBLL infoBLL = new ProfileInfoBLL();
            CMS_ProfileInfo info = infoBLL.GetLastProfileInfoByProfileCode(profileCode);
            if (info != null)
            {
                // Load patient info
                PatientInfoBLL patientBLL = new PatientInfoBLL();
                PatientInfo patient = patientBLL.GetPatientInfoByProfileInfoCode(info.ProfileInfoCode);
                if (patient != null)
                {
                    txtPatientName.Text = patient.PatientName;
                    txtDOB.Text = patient.Birthday.ToShortDateString();
                    ddlGender.SelectedValue = patient.Gender;
                    txtIDNumber.Text = patient.IDNumber;
                    txtPermAddr.Text = patient.PermanentAddress;
                    txtTempAddr.Text = patient.TemporaryAddress;
                    txtMobileNumber.Text = patient.MobileNumber;
                    txtPhoneNumber.Text = patient.PhoneNumber;
                    txtRepresentativeName.Text = patient.EmergencyRepresentative;
                    txtRepresentativePhone.Text = patient.RepresentativePhoneNumber;
                    txtLocalContactName.Text = patient.LocalContactPoint;
                    txtLocalContactPhone.Text = patient.LocalContactPointPhoneNumber;
                    txtMedicalStaffName.Text = patient.LocalMedicalStaff;
                    txtMedicalStaffPhone.Text = patient.LocalMedicalStaffPhoneNumber;
                    txtMedicalFacility.Text = patient.MedicalFacility;
                    txtNote.Text = patient.Note;
                }

                // Load medical info
                MedicalInfoBLL medicalBLL = new MedicalInfoBLL();
                MedicalInfo medical = medicalBLL.GetMedicalInfoByProfileInfoCode(info.ProfileInfoCode);
                if (medical != null)
                {
                    txtDiagnosis.Text = medical.Diagnosis;
                    txtMedicalTime.Text = medical.MedicalTime.ToShortDateString();
                    txtTreatmentMethod.Text = medical.TreatmentMethod;
                    txtSituation.Text = medical.Situation;
                    txtSupport.Text = medical.Support;
                }


                // Load info supplier info
                InfoSupplierInfoBLL infoSupplierBLL = new InfoSupplierInfoBLL();
                InfoSupplierInfo supplier = infoSupplierBLL.GetInfoSupplierInfoByProfileInfoCode(info.ProfileInfoCode);
                if (supplier != null)
                {
                    txtInfoSupplierName.Text = supplier.SupplierName;
                    txtInfoSupplierPhone.Text = supplier.PhoneNumber;
                    //txtPersonalCode.Text = supplier.PersonalCode;
                    txtEmail.Text = supplier.Email;
                }
            }
        }*/

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Session["User"] != null)
            {
                string sessionVideoUrl = VideoUrl;
                string sessionImageUrl = ImageUrl;

                Session["imageArrayPath"] = null;
                Session["videoArrayPath"] = null;

                String Proposal = Session["User"].ToString();

                DateTime dayOfBirth = new DateTime();

                try
                {
                    dayOfBirth = Convert.ToDateTime(txtDOB.Text);
                }
                catch
                {
                    dayOfBirth = DateTime.Now;
                }

                PatientInfo patient = new PatientInfo(txtPatientName.Text, dayOfBirth,
                    ddlGender.SelectedValue, txtIDNumber.Text, txtPermAddr.Text, txtTempAddr.Text,
                    txtMobileNumber.Text, txtPhoneNumber.Text, txtRepresentativeName.Text,
                    txtRepresentativePhone.Text, txtLocalContactName.Text, txtLocalContactPhone.Text,
                    txtMedicalStaffName.Text, txtMedicalStaffPhone.Text, txtMedicalFacility.Text,
                    txtNote.Text);


                DateTime medicalTime = new DateTime();

                try
                {
                    medicalTime = Convert.ToDateTime(txtMedicalTime.Text);
                }
                catch
                {
                    medicalTime = DateTime.Now;
                }

                MedicalInfo medical = new MedicalInfo(txtDiagnosis.Text, medicalTime,
                    txtTreatmentMethod.Text, txtSituation.Text, txtSupport.Text, null);
                InfoSupplierInfo supplier = new InfoSupplierInfo(txtInfoSupplierName.Text,
                    txtInfoSupplierPhone.Text, Proposal, txtEmail.Text, null);

                DateTime createdDate = new DateTime();

                try
                {
                    if (!string.IsNullOrEmpty(txtCreatedDate.Text))
                        createdDate = Convert.ToDateTime(txtCreatedDate.Text);
                }
                catch
                {
                    createdDate = DateTime.Now;
                }

                ProfileInfo profileInfo = new ProfileInfo();
                Profile profile = new Profile();
                profile.ProfileInfo = profileInfo;
                profile.PatientInfo = patient;
                profile.MedicalInfo = medical;
                profile.InfoSupplierInfo = supplier;
                ProfileBLL profileBLL = new ProfileBLL();
                bool result = profileBLL.InsertProfileInfoWithMedia(profile, sessionVideoUrl, sessionImageUrl, string.Empty, Proposal, createdDate, txtDescription.Text);

                if (result)
                {
                    Response.Redirect("Default.aspx");
                }
            }
        }
        private string _imageUrl;
        public string ImageUrl
        {
            get
            {
                imageArrayPath = (Session["imageArrayPath"] as List<string>);
                if (imageArrayPath != null && imageArrayPath.Count > 0)
                {
                    foreach (var item in imageArrayPath)
                    {
                        _imageUrl += item;
                        break;
                    }
                    return _imageUrl;
                }
                else
                {
                    return string.Empty;
                }
            }
            set { _imageUrl = value; }
        }

        private string _videoUrl;
        public string VideoUrl
        {
            get
            {
                videoArrayPath = (Session["videoArrayPath"] as List<string>);
                if (videoArrayPath != null && videoArrayPath.Count != 0)
                {
                    foreach (var item in videoArrayPath)
                    {
                        _videoUrl += item;
                        break;
                    }
                    return _videoUrl;
                }
                else
                {
                    return string.Empty;
                }
            }
            set { _videoUrl = value; }
        }
    }
}